//路由配置文件
import { createRouter, createWebHashHistory } from "vue-router";
//单独提取侧边菜单渲染所需要的路由数据包
import store from "@/store";
export const mainRoutes = [
  {
    path: "/dashboard",
    component: () => import("../views/dashboard/dashboard.vue"),
    meta: {
      label: "数据可视化",
      icon: "PieChart",
    },
  },
  {
    path: "/category",
    component: () => import("../views/category/index.vue"),
    meta: {
      label: "分类管理",
      icon: "Grid",
    },
    children: [
      {
        path: "/category/list",
        component: () => import("../views/category/list.vue"),
        meta: {
          label: "分类列表",
          icon: "",
        },
      },
      {
        path: "/category/pub",
        component: () => import("../views/category/pub.vue"),
        meta: {
          label: "发布分类",
          icon: "",
        },
      },
    ],
  },
  {
    path: "/company",
    component: () => import("../views/company/pub.vue"),
    meta: {
      label: "企业入驻",
      icon: "OfficeBuilding",
    },
  },
  {
    path: "/job",
    component: () => import("../views/job/index.vue"),
    meta: {
      label: "岗位管理",
      icon: "Bowl",
    },
    children: [
      {
        path: "/job/list",
        component: () => import("../views/job/list.vue"),
        meta: {
          label: "岗位列表",
          icon: "",
        },
      },
    ],
  },
  {
    path: "/manager",
    component: () => import("../views/manager/index.vue"),
    meta: {
      label: "权限管理",
      icon: "Memo",
    },
    children: [
      {
        path: "/manager/role",
        component: () => import("../views/manager/role.vue"),
        meta: {
          label: "角色管理",
          icon: "",
        },
      },
      {
        path: "/manager/user",
        component: () => import("../views/manager/user.vue"),
        meta: {
          label: "账号管理",
          icon: "",
        },
      },
    ],
  },
  {
    path: "/excel",
    component: () => import("../views/excel/index.vue"),
    meta: {
      label: "Excel导入导出",
      icon: "Postcard",
    },
    children: [
      {
        path: "/excel/out",
        component: () => import("../views/excel/out.vue"),
        meta: {
          label: "Excel导出",
          icon: "",
        },
      },
      {
        path: "/excel/in",
        component: () => import("../views/excel/in.vue"),
        meta: {
          label: "Excel导入",
          icon: "",
        },
      },
    ],
  },
];
let isLogin = false; //模拟登录状态
const router = createRouter({
  history: createWebHashHistory(), //路由模式，哈希模式
  //路由映射关系配置 （路径---组件）
  routes: [
    {
      path: "/",
      component: () => import("../layout/index.vue"),
      children: mainRoutes,
    },
    {
      path: "/login",
      component: () => import("../views/login.vue"),
    },
    {
      path: "/test/vuex",
      component: () => import("../views/test-vuex/index.vue"),
    },
    {
      path: "/permit",
      component: () => import("../views/permit.vue"),
    },
  ],
});

// 全局路由守卫

router.beforeEach((to, from, next) => {
  // to 想访问的路由
  // from  从哪个路由来的
  // next  放行的方法函数
  // console.log(to);
  let whiteList = ["/login", "/permit"];
  if (!whiteList.includes(to.path)) {
    if (store.state.user.userInfo) {
      let { permission } = store.state.user.userInfo;
      let permiss = permission.concat("/");
      if (permiss.includes(to.path)) {
        next(); //有权限
      } else {
        next("/permit"); //无权限
      }
    } else {
      next("/login"); //未登录用户，强制跳转登录页
    }
  } else {
    next();
  }
});

export default router;
